program ChromeBlame;

include <core.vhs>
include <shell.vhs>

import core::*;
import core::Pattern::*;
import core::Logging::*;

const command = '#########
if [ -f $output/blame/$ffile.0 ]
then
	echo "Skip $folder"
else
	echo "Annotate $folder..."
	rm $output/temp-blame.xml
	svn blame --incremental --xml $folder/*.* > $output/temp-blame.xml	
	if [ -f $output/temp-blame.xml ]; then
		echo "done" > $output/blame/$ffile.0
		if [ -f $output/blame.xml ]; then
			cat $output/temp-blame.xml >> $output/blame.xml
		else
			cp $output/temp-blame.xml $output/blame.xml
		fi
	else
		echo "Error annotating directory $folder" >> gcco-error.txt
	fi
fi';

function main(input='1.0.154.65', output='~/analysis/chrome/' + input, base='/storage/vhnguyen/chrome/') {
	var outfile = new OutFile(format("blame-%s.sh", input))
	outfile.writeln("#!/bin/bash");
	outfile.writeln("output=" + output);

	outfile.writeln("[ -d $output ] && echo $output is existed. ||  mkdir $output");
	outfile.writeln("[ -d $output/blame ] && echo $output/blame is existed. ||  mkdir $output/blame");
	
	outfile.writeln("cd " + base + input);
	
	var currentFile = new File(base + input).getCanonicalPath();
	
	println("Input folder: %s", currentFile);
	println("Output folder: %s", output);
	
	shell::directoryTravesal(currentFile, excludePattern=@'\\.svn', func=function(x) {
		var folder =  new Folder(x.getCanonicalPath());
		var files = folder.scanFiles(@".+?\.(c|cc|cxx|cpp|h|hh|hxx|hpp)$", false);
		if (files.size() > 0) {
			var path = folder.getCanonicalPath();
			if (path.length() == currentFile.length())
				path = ".";
			else
				path = path.substring(currentFile.length() + 1);
			outfile.writeln(command.replace("$ffile", path.replace('/','_')).replace("$folder", path));
		}
	});
}